//
//  JXLandTextField.m
//
//
//  Created by YuSong.Yan on 15/10/13.
//
//

#import "JXLandTextField.h"

#define kTextSucureIcon         JXImage(@"pwd_Secure.png")
#define kTextUnSucureIcon       JXImage(@"pwd_unSecure.png")
#define kDefaultBorderColor     [UIColor colorWithWhite:191.0f / 255.0f alpha:1.0]

@interface JXLandTextField ()<UITextFieldDelegate>
@property (nonatomic, assign) JXTextFieldHeadType headType;
@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIImageView *headIconView;
@property (nonatomic, strong) UILabel *headLabel;
@property (nonatomic, strong) UIButton *secureBtn;

@end

@implementation JXLandTextField

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _isSecure = NO;
        [self initSubviews];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame HeadType:(JXTextFieldHeadType)type
{
    self = [super initWithFrame:frame];
    if (self) {
        self.headType = type;
        if (JXTextFieldHeadTypeSecure == type){
            _isSecure = YES;
        }else{
            _isSecure = NO;
        }
        self.textFontSize = 14;
        self.headFontSize = 15;
        self.activeBorderColor = [UIColor greenColor];
        self.unActiveBorderColor = kDefaultBorderColor;
        [self initSubviews];
    }
    return self;
}

- (void)setIsSecure:(BOOL)isSecure
{
    _isSecure = isSecure;
    if (_isSecure) {
        [self.secureBtn setImage:kTextSucureIcon forState:UIControlStateNormal];
    }else{
        [self.secureBtn setImage:kTextUnSucureIcon forState:UIControlStateNormal];
    }
    self.textField.secureTextEntry = _isSecure;
}

- (NSString *)text
{
    NSString *text = self.textField.text;
    return text;
}

- (void) setText:(NSString *)text {
    self.textField.text = text;
}

#pragma mark -
#pragma mark - init subviews

- (void)initSubviews
{
    self.backgroundView = [[UIView alloc] initWithFrame:self.bounds];
    self.backgroundView.backgroundColor = kTextViewContentDefaultColor;//[UIColor whiteColor];
    [self addSubview:self.backgroundView];

    if (JXTextFieldHeadTypeIcon == self.headType)
    {
        self.headIconView = [[UIImageView alloc] initWithFrame:CGRectZero];
        self.headIconView.backgroundColor = [UIColor clearColor];
        self.headIconView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:self.headIconView ];
    }else{
        self.headLabel  = [[UILabel alloc] init];
        self.headLabel.textAlignment = NSTextAlignmentLeft;
        self.headLabel.backgroundColor = [UIColor clearColor];
        self.headLabel.numberOfLines = 1;
        self.headLabel.lineBreakMode = NSLineBreakByTruncatingTail;
        [self addSubview:self.headLabel];
    }

    self.textField = [[UITextField alloc] init];
    self.textField.backgroundColor = [UIColor clearColor];
    self.textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    self.textField.delegate = self;
    self.textField.autocorrectionType = UITextAutocorrectionTypeNo;
    self.textField.autocapitalizationType  =  UITextAutocapitalizationTypeNone;
    self.textField.returnKeyType = UIReturnKeyGo;
    self.textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    [self addSubview:self.textField];

    if (JXTextFieldHeadTypeSecure == self.headType || self.isSecure){
        self.textField.secureTextEntry = YES;
        _secureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _secureBtn.titleLabel.font = JXSystemFont(14.0);
        [_secureBtn setImage:kTextSucureIcon forState:UIControlStateNormal];
        [_secureBtn addTarget:self
                       action:@selector(secureBtnTouched:)
             forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_secureBtn];
    }
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    self.layer.cornerRadius = 2;
    CGRect rect = self.frame;

    self.backgroundView.layer.cornerRadius = self.layer.cornerRadius;
    self.backgroundView.layer.masksToBounds = YES;
    self.backgroundView.layer.borderWidth = JXMinPixels;
    if ( [self.textField isFirstResponder])
    {
        self.backgroundView.layer.borderColor = self.activeBorderColor.CGColor;
    }else{
        self.backgroundView.layer.borderColor = self.unActiveBorderColor.CGColor;
    }

    self.backgroundView.frame = CGRectMake(0,
                                           0,
                                           rect.size.width,
                                           rect.size.height);

    if (JXTextFieldHeadTypeText == self.headType){
        self.headLabel.font = [UIFont systemFontOfSize:self.headFontSize];
        float headLabelFontPointSize = [self.headLabel.font pointSize];
        float headLength = [self.headTitle length];
        float headLabelWidth = headLabelFontPointSize * headLength + 10;
        self.headLabel.frame = CGRectMake(6, 1, headLabelWidth, rect.size.height - 2);
        self.headLabel.layer.cornerRadius = self.layer.cornerRadius;
        self.headLabel.text = self.headTitle;

        self.textField.frame = CGRectMake(CGRectGetMaxX(self.headLabel.frame) + 4,
                                          0,
                                          rect.size.width - CGRectGetMaxX(self.headLabel.frame)-6,
                                          rect.size.height);
    }else if (JXTextFieldHeadTypeIcon == self.headType){
        CGFloat space = 5.0;
        self.headIconView.frame = CGRectMake(space, (rect.size.height - 18) / 2, 18, 18);
        self.headIconView.image = self.headIcon;

        self.textField.frame = CGRectMake(self.headIconView.frame.size.width + 2*space,
                                          0,
                                          rect.size.width - self.headIconView.frame.size.width  - 2*space ,
                                          rect.size.height);
    }else{
        self.headLabel.font = [UIFont systemFontOfSize:self.headFontSize];
        float headLabelFontPointSize = [self.headLabel.font pointSize];
        float headLength = [self.headTitle length];
        float headLabelWidth = headLabelFontPointSize * headLength + 10;
        self.headLabel.frame = CGRectMake(6, 1, headLabelWidth, rect.size.height - 2);
        self.headLabel.layer.cornerRadius = self.layer.cornerRadius;
        self.headLabel.text = self.headTitle;

        self.textField.frame = CGRectMake(self.headLabel.right, 0, self.width- self.height - self.headLabel.right , rect.size.height);
        self.secureBtn.frame = CGRectMake(_textField.right, 0 , self.height, self.height);
        //        self.secureBtn.backgroundColor = [UIColor cyanColor];
    }

    self.textField.font = [UIFont systemFontOfSize:self.textFontSize];
    self.textField.placeholder = self.placeholder;

}


- (void)becomeFirstInputResponder
{
    [self.textField becomeFirstResponder];
}


- (void)resignFirstInputResponder
{
    [self.textField resignFirstResponder];
    //    self.backgroundView.layer.borderColor = self.unActiveBorderColor.CGColor;
}


#pragma mark -
#pragma mark - Ui response method

- (void)secureBtnTouched:(UIButton *)sender
{
    if (_isSecure)
    {
        self.textField.secureTextEntry = NO;
    }else{
        self.textField.secureTextEntry = YES;
    }
    self.isSecure = !_isSecure;
}

#pragma mark -
#pragma mark - set method

- (void)setTextFiledHeadTitle:(NSString *)title Color:(UIColor *)color
{
    self.headTitle = title;
    self.headLabel.text = title;
    self.headLabel.textColor = color;
}

- (void)setTextFiledHeadIcon:(UIImage *)icon Color:(UIColor *)color
{
    self.headIcon = icon;
    self.headIconView.image = self.headIcon;
}
#pragma mark -
#pragma mark - textfeild

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

    self.backgroundView.layer.borderColor = self.activeBorderColor.CGColor;
    return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField           // became first responder
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldDidBeginEditing:)])
    {
        [self.delegate textFieldDidBeginEditing:self];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    //    return [self.delegate textFieldShouldReturn:textField];
    self.text = textField.text;

    if (self.delegate && [self.delegate respondsToSelector:@selector(textFieldDidBeginEditing:)])
    {
        [self.delegate textFieldReturnBtnTouched:self];
    }
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField
{
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string;  {

    self.text = textField.text;
    //    if (self.delegate && [self.delegate respondsToSelector:@selector(textFiledFinisedEdit:)])
    //    {
    //       return [self.delegate textFiledFinisedEdit:self];
    //    }
    //
    //    return NO;

    return YES;
}



- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.text = textField.text;
    self.backgroundView.layer.borderColor = self.unActiveBorderColor.CGColor;

    if (self.delegate && [self.delegate respondsToSelector:@selector(textFiledFinisedEdit:)])
    {
        [self.delegate textFiledFinisedEdit:self];
    }

    JXLog(@"text did end edit!");
}


@end
